/**
 * 语音播报
 */
var player;
var name;
var videopath;
var uuid;
var isGetPro=true;
$(function(){
	player=$("#jquery_jplayer_1").jPlayer({
		swfPath:$Path+"/public/javascripts/flash/jquery.jplayer.swf",
//		solution: "flash, html",
		solution: "html, flash",
		supplied: "mp3",
		ended:function(){
			if(videopath!=null&&videopath!=""){
				getBroadNext(name,videopath);
			}
			else{
				openBeginButton();
				openClearButton();
			}
		}
	});
	//延迟加载
	var tmpinterVal=setInterval(function(){
		var tone=$("#tone").combobox('getValue');
		var speed=$("#speed").combobox('getValue');
		if(tone!=undefined&&speed!=undefined){
			var firstKey=$("#firstKey").val();
			if(firstKey!=null&&firstKey!=""&&firstKey!=undefined){
				changeContent(firstKey);
			}
			window.clearInterval(tmpinterVal);
		}
	},1000);
})

function getBroadNext(filename,vidpath){
	$.ajax({
		url:$Path+"/admin/broadcast/getnext",
		type:'get',
		data:{name:filename,path:vidpath},
		success:function(data){
			if(data.code==200){
				videopath=$Path+data.results;
				play(videopath);
			}
			else if(data.code==600){
				videopath=="";
				openBeginButton();
				openClearButton();
			}
		},
		error:function(data){
			videopath="";
		}
	})
}
/**
 * 打开播放器
 * @param path
 */
function play(path){
	player.jPlayer("setMedia", {
		title: "Bubble",
		mp3:$Path+"/"+path
	});
	player.jPlayer("play");
	
}
/**
 * 开始播放
 */
var isBroad=99;
var wordid="";
var time=0;
//function beginBroad(){
//	var content=$("#content").val();
//	var auth=window.parent.getAuth();
//	var tone=$("#tone").combobox('getValue');
//	var speed=$("#speed").combobox('getValue');
//	if(content==null||content==''||content==undefined){
//		parent.$.messager.alert('失败','播报内容不能为空','error');
//		return;
//	}
//	parent.$.messager.confirm('播报','您确定要进行播报吗？',function(data){
//		if(data){
//			window.parent.showloading();
//			$.ajax({
//				type:"post",
//				data:{content:content,wordid:wordid,tone:tone,speed:speed,authenticityToken:auth},
//				url:$Path+'/admin/broadcast/broadstart',
//				success:function(data){
//					window.parent.hideloading();
//					if(data.code==200){
//						videopath=data.results.videoPath;
//						name=data.results.fileName;
//						play(data.results.videoPath);
//						colseBeginButton();
//						colseClearButton();
//					}
//					else{
//						parent.$.messager.alert('失败',data.msg,'error');
//					}
//				},
//				error:function(data){
//					window.parent.hideloading();
//					parent.$.messager.alert('失败',"网络错误",'error');
//				}
//			})
//		}
//	})
//}
//合成
function beginBroad(){
	var content=$("#content").val();
	if(content==''||content==null||content==undefined){
		$.messager.alert('警告','内容为空','warning');
		return;
	}
	var tone=$("#tone").combobox('getValue');
	var speed=$("#speed").combobox('getValue');
	uuid=UUID.prototype.createUUID();
	var auth=window.parent.getAuth();
	$.messager.progress({
		title:'合成中',
		msg:'合成中，请稍候......',
		interval:10000000000000
	});
	$.messager.progress('bar').progressbar({
       onChange: function(value){
//           if(value == 100){
//              $.messager.progress('close');
//           }
       }
	});
	getProcess();
	$.ajax({
		type:'post',
		data:{content:content,tone:tone,speed:speed,uuid:uuid,authenticityToken:auth},
		url:$Path+'/admin/broadcast/broadstart',
		success:function(data){
			if(data.code==200){
				$.messager.progress('bar').progressbar('setValue',100);
				$.messager.progress('close');
				path=$Path+"/public/filetemp/"+uuid+".mp3";
				player.jPlayer("setMedia", {
					title: "Bubble",
					mp3:path
				});
				player.jPlayer("play");
			}else{
				$.messager.progress('close');
				isGetPro=false;
				$.messager.alert('警告',data.msg,'warning');
			}
		},error:function(){
			$.messager.progress('close');
			isGetPro=false;
			$.messager.alert('警告',"服务器内部错误",'warning');
		}
	})
}

/**
 * 获取进度
 */
function getProcess(){
	if(!isGetPro){
		return;
	}
	//延迟100ms
	setTimeout(function(){
	},500)
	$.ajax({
		type:'GET',
		url:$Path+'/admin/broadcast/getProcess',
		data:{uuid:uuid},
		success:function(data){
			if(data!=''&&data!=undefined){
				var pro=Math.floor(data);
				if(data==100||data=='100'){
					$.messager.progress('bar').progressbar('setValue',99);
				}
				else{
					$.messager.progress('bar').progressbar('setValue',pro);
					getProcess();
				}
			}else{
				getProcess();
			}
		},error:function(){
			getProcess();
		}
	})
}


function openBeginButton(){
	$("#begin").removeClass("colseMouseClass");
	$("#begin").addClass("openMouseClass");
	$("#begin").removeAttr("disabled");
}

function colseBeginButton(){
	$("#begin").removeClass("openMouseClass");
	$("#begin").addClass("colseMouseClass");
	$("#begin").attr("disabled","disabled");
}
function openClearButton(){
	$("#clear").removeClass("colseMouseClass");
	$("#clear").addClass("openMouseClass");
	$("#clear").removeAttr("disabled");
}

function colseClearButton(){
	$("#clear").removeClass("openMouseClass");
	$("#clear").addClass("colseMouseClass");
	$("#clear").attr("disabled","disabled");
}



/**
 * 清空文字
 */
function clearBroad(){
	$("#content").val("");
}
/**
 * 暂停
 */
function pauseBroad(){
	player.jPlayer("pause");
}
/**
 * 开始
 */
function playBroad(){
	player.jPlayer("play");
}
/**
 * 切换右侧内容
 * @param content
 * @param id
 */
function changeContent(id){
	$.ajax({
		type:'GET',
		url:$Path+'/admin/broadcast/getContent',
		data:{id:id},
		success:function(data){
			if(data.code==200){
				$("#content").val(data.results.content);
				$("#tone").combobox('setValue',data.results.tone);
				$("#speed").combobox('setValue',data.results.speed);
				$($("#"+id).siblings()).removeClass('active');		
				$("#"+id).addClass('active');
				wordid=id;
			}
			else{
				parent.$.messager.alert('失败',data.msg,'error');
			}
		}
	})
}
/**
 * 删除播放文本
 */
function delkeyword(wordid){
	if(wordid==''||wordid==null||wordid==undefined){
		parent.$.messager.alert('失败',"请选择要删除的播报记录",'error');
		return;
	}
	parent.$.messager.confirm('删除','您确定要删除播报记录吗？',function(data){
		if(data){
			window.parent.showloading();
			var auth=window.parent.getAuth();
			$.ajax({
				type:"post",
				data:{id:wordid,authenticityToken:auth},
				url:$Path+'/admin/broadcast/deleteBroad',
				success:function(data){
					window.parent.hideloading();
					if(data.code==200){
						window.location.reload();
					}
					else{
						parent.$.messager.alert('失败',data.msg,'error');
					}
				},
				error:function(data){
					window.parent.hideloading();
					parent.$.messager.alert('失败',"网络错误",'error');
				}
			})
		}
	})
}
/**
 * 保存为常用文本
 */
function saveTextBtn(){
	var cast={};
	cast.content=$("#content").val();
	var auth=window.parent.getAuth();
	if(cast.content==null||cast.content==''||cast.content==undefined){
		parent.$.messager.alert('失败','播报内容不能为空','error');
		return;
	}
	cast.title=getTitle(cast.content);
	window.parent.showloading();
	cast.tone=$("#tone").combobox('getValue');
	cast.speed=$("#speed").combobox('getValue');
	$.ajax({
		type:"post",
		data:{cast:cast,authenticityToken:auth},
		url:$Path+'/admin/broadcast/saveBroad',
		success:function(data){
			window.parent.hideloading();
			if(data.code==200){
				window.location.reload();
			}
			else{
				parent.$.messager.alert('失败',data.msg,'error');
			}
		},
		error:function(data){
			window.parent.hideloading();
			parent.$.messager.alert('失败',"网络错误",'error');
		}
	})
}
/**
 * 获取标题
 * @param content
 */
function getTitle(content){
	if(content==null||content==''||content==undefined){
		return "";
	}
	var text=content.split("\n");
	var title=text[0];
	if(title.length>8){
		title=title.substr(0,8);
	}
	return title;
}

